/* $Header: run_ui.c,v 2.2 01/10/02 10:32:34 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME:  run_ui.c                                              */
/* ============                                                        */
/*                                                                     */
/* Author:  Lesley Freed                                               */
/*                                                                     */
/* MODULE PURPOSE:  To provide Get, Set and Check functions for the    */
/*                  user interface and command system for run control. */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <error.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <ramps.h>
#include <zone_config.h>
#include <config.h>
#include <zone_status.h>
#include <status.h>
#include <zone_ui.h>
#include <run_ui.h>
#include <cmd_set.h>
#include <i_op.h>
#include <readiness.h>
#include <sequence.h>
#include <seq_ui.h>
#include <iomap.h>
#include <leds.h>

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/

/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/

/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/

extern MV_FLAG_TYPE MVPending;

/*****************************/
/*   LOCAL DEFINES           */
/*****************************/

/*****************************/
/*   STATIC VARIABLES        */
/*****************************/

/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/

BIT8 RunInProgress( void )
{
   if ( Run_State == RUN_ACTIVE )
   {
      return( TRUE );
   }
   else
   {
      return( FALSE );
   }
}

/***********************************************************************/

BIT8 RunInProgressOrPending( void )
{
   if ( Run_Requested || Run_State == RUN_ACTIVE )
   {
      return( TRUE );
   }
   else
   {
      return( FALSE );
   }
}

/***********************************************************************/

BIT8 RunActivePendingOrPost( void )
{
   if ( ( RunInProgressOrPending() == TRUE ) || ( InPostTime() == TRUE ) )
   {
      return( TRUE );
   }
   else
   {
      return( FALSE );
   }
}

/***********************************************************************/

BIT8 RunActiveOrPostTime( void )
{
   if ( ( RunInProgress() == TRUE ) || ( InPostTime() == TRUE ) )
   {
      return( TRUE );
   }
   else
   {
      return( FALSE );
   }
}

/***********************************************************************/

TIME_SETPT GetRunLength( void )
{
   return( MilliSecsToTime( Run_Length ) );
}

/***********************************************************************/

TIME_SETPT GetRunTime( void )
{
   return( MilliSecsToTime( Run_Clock ) );
}

/***********************************************************************/

TIME_SETPT GetLastRunTime ( void )
{
     return ( MilliSecsToTime ( Last_Run_Time ));
}

/***********************************************************************/

BIT8 InPostTime ( void )
{
     if ( Run_State == POST_RUN )
     {
          return TRUE;
     }
     else
     {
          return FALSE;
     }
}

/***********************************************************************/

TIME_SETPT PostTimeRemaining ( void )
{
     if (( Run_State == POST_RUN ) && ( Run_Clock < (INT32)Active_Wkfile.post_time ))
     {
          return( MilliSecsToTime( Active_Wkfile.post_time - Run_Clock ) );
     }
     else
     {
          return( MilliSecsToTime( Active_Wkfile.post_time ) );
     }
}

/***********************************************************************/

BIT8 NotRunIdle( void )
{
   if ( Run_State == RUN_IDLE )
   {
      return( FALSE );
   }

   return( TRUE );
}

/***********************************************************************/

ON_OFF GetAutoPrepRun(void)
{
    return Auto_Prep_Run;
}

/***********************************************************************/

UI_ERR SetAutoPrepRun( ON_OFF state )
{
        if (state == OFF)
        {
                Auto_Prep_Run = state;

                /* if we are in PRE_RUN and not in an Scc_Run or sequence then go to RUN_IDLE */
                INTERRUPTS_OFF
                if ( (Run_State == PRE_RUN) && !Scc_Run && !SequenceInProgress() )
                {
                        StopRun(Abort);
                }
                INTERRUPTS_ON

                return OK;
        }
        else if (state == ON)
        {
                Auto_Prep_Run = state;

                /* if we are in RUN_IDLE and not in a sequence then go to PRE_RUN */
                INTERRUPTS_OFF
                if ( (Run_State == RUN_IDLE) && !SequenceInProgress() )
                {
                        Prepare(Auto);
                }
                INTERRUPTS_ON

                return OK;
        }
        else
        {
                return INVALID_PARAM;
        }
}

/***********************************************************************/

/*
** SetControlledByHost - set whether or not IQ is in "INET mode"
**      mode == 0 -> leave "INET mode"
**      mode == 1 -> enter "INET mode"
*/

UI_ERR SetControlledByHost(INT32 mode)
{
        UI_ERR retval = OK;  /* default */

        if (mode == 1)
        {
                /* force GC run state to RUN_IDLE but do NOT abort sequence/poweron recovery or assert APG Stop signal */
                (void)SetRunState(0, FALSE);

                /* force host ready to FALSE to hold things off until the host can supply the true value */
                SetHostReady(FALSE);

                /* illuminate front panel indicator */
//          		TURN_REMOTE_LED_ON();

                /* set "INET mode" */
                Controlled_By_Host = TRUE;

                /* No OEM_det alias for 339x host   */
                DetectorA_Config.host_model = TYPE_13_14_AS_IS;
                DetectorB_Config.host_model = TYPE_13_14_AS_IS;

        }
        else if (mode == 0)
        {
                /* clear host ready */
                SetHostReady(TRUE);

		/* extinguish front panel indicator */
//		TURN_REMOTE_LED_OFF();

                /* clear "INET mode" */
                Controlled_By_Host = FALSE;
        }
        else if (mode > 1)
        {
                retval = PARAM_TOO_LARGE;
        }
        else
        {
                retval = PARAM_TOO_SMALL;
        }

        return retval;
}
